package models

import (
	"encoding/json"
	"time"
)

// base definition
const (
	MessageOnlineInterval = int64(time.Minute * 3)
	MessageStatusInterval = int64(time.Minute * 5)

	EdgeGatewayDeviceID = "" // 空字符串表示是服务类消息
)

// device types
const (
	DeviceTypeNVR      = "NVR"
	DeviceTypeCamera   = "Camera"
	DeviceTypeNoNVR    = 1
	DeviceTypeNoCamera = 0
)

// device channels
const (
	DeviceChannelDirect = -1
	DeviceChannelNVR    = -2
)

// device reboot date type
const (
	DeviceRebootDateTypeOff = "off" // 关闭
	DeviceRebootDateTypeD1  = "d1"  // 每天
	DeviceRebootDateTypeW0  = "w0"  // 每星期日
	DeviceRebootDateTypeW1  = "w1"  // 每星期一
	DeviceRebootDateTypeW2  = "w2"  // 每星期二
	DeviceRebootDateTypeW3  = "w3"  // 每星期三
	DeviceRebootDateTypeW4  = "w4"  // 每星期四
	DeviceRebootDateTypeW5  = "w5"  // 每星期五
	DeviceRebootDateTypeW6  = "w6"  // 每星期六
)

// DeviceRebootWeekMap for week map
var DeviceRebootWeekMap = map[time.Weekday]string{
	time.Sunday:    DeviceRebootDateTypeW0,
	time.Monday:    DeviceRebootDateTypeW1,
	time.Tuesday:   DeviceRebootDateTypeW2,
	time.Wednesday: DeviceRebootDateTypeW3,
	time.Thursday:  DeviceRebootDateTypeW4,
	time.Friday:    DeviceRebootDateTypeW5,
	time.Saturday:  DeviceRebootDateTypeW6,
}

// vendors
const (
	VendorHAIKANG     = 1
	VendorDAHUA       = 2
	VendorHUAWEI      = 3
	VendorYUSHI       = 4
	VendorTIANDIWEIYE = 5
	VendorGUANGQI     = 6
)

// VendorMap vendor map
var VendorMap = map[int]string{
	VendorHAIKANG:     "海康",
	VendorDAHUA:       "大华",
	VendorHUAWEI:      "华为",
	VendorYUSHI:       "宇视",
	VendorTIANDIWEIYE: "天地伟业",
	VendorGUANGQI:     "光启",
}

// category types
const (
	CategoryBALL    = 1
	CategoryGUN     = 2
	CategoryTHERMAL = 3
	CategoryFULL    = 4
	CategoryUAV     = 5
	CategoryMOBILE  = 6
	CategoryUNKNOWN = 9
)

// CategoryMap category map
var CategoryMap = map[int]string{
	CategoryBALL:    "球机",
	CategoryGUN:     "枪机",
	CategoryTHERMAL: "热成像",
	CategoryFULL:    "全景",
	CategoryUAV:     "无人机",
	CategoryMOBILE:  "单兵(执法记录仪)",
	CategoryUNKNOWN: "未知",
}

// DevicesEvent defination
type DevicesEvent struct {
	Cameras []*CamerasEvent `json:"cameras"`
	NVRs    []*NVREvent     `json:"nvrs"`
}

// CamerasEvent defination
type CamerasEvent struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Channel  int    `json:"channel"`
	IP       string `json:"ip"`
	NVRID    string `json:"nvrid"`
	Password string `json:"password"`
	Port     int    `json:"port"`
	Rtspport int    `json:"rtspport"`
	Type     int    `json:"type"`
	Username string `json:"username"`
}

// NVREvent defination
type NVREvent struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	IP          string `json:"ip"`
	Password    string `json:"password"`
	Playbackcnt int    `json:"playbackcnt"`
	Port        int    `json:"port"`
	Rtspport    int    `json:"rtspport"`
	Type        int    `json:"type"`
	Username    string `json:"username"`
}

// Devices definition
type Devices struct {
	Cameras []*Camera `json:"cameras"`
	NVRs    []*NVR    `json:"nvrs"`
}

// Device definition
type Device struct {
	ID            string `json:"id"`
	Online        bool   `json:"online"`
	ValidateInfo  string `json:"validateInfo"`
	Type          string `json:"type,omitempty"`
	SourceType    string `json:"sourceType,omitempty"`
	ModelID       string `json:"modelId"`
	ProjectID     string `json:"projectId,omitempty"`
	GatewayID     string `json:"gatewayId,omitempty"`
	GroupID       string `json:"groupId,omitempty"`
	TenantID      string `json:"tenantId,omitempty"`
	Name          string `json:"name"`
	Vendor        int    `json:"vendor,omitempty"`
	VendorName    string `json:"vendorName,omitempty"`
	Category      int    `json:"category,omitempty"`
	CategoryValue int    `json:"categoryValue,omitempty"`
	Version       string `json:"version,omitempty"`
	RebootDate    string `json:"rebootDate,omitempty"`
	RebootTime    string `json:"rebootTime,omitempty"`
}

// Camera device
type Camera struct {
	Device
	Attributes *CameraAttributes `json:"attributes"`
}

// CameraAttributes attr
type CameraAttributes struct {
	Channel   int    `json:"channel"`
	IP        string `json:"ip"`
	NvrID     string `json:"nvrid"`
	Password  string `json:"password"`
	Port      int    `json:"port"`
	Rtspport  int    `json:"rtspport"`
	Frequency int    `json:"frequency"`
	Ifsnaping bool   `json:"ifsnaping"`
	Snapshot  string `json:"snapshot"`
	Type      int    `json:"type"`
	Username  string `json:"username"`
}

func (c *Camera) String() string {
	buf, _ := json.Marshal(c)

	return string(buf)
}

// NVR device
type NVR struct {
	Device
	Attributes *NVRAttributes `json:"attributes"`
}

// NVRAttributes attr
type NVRAttributes struct {
	GatewayID   string `json:"gatewayid"`
	IP          string `json:"ip"`
	Password    string `json:"password"`
	Playbackcnt int    `json:"playbackcnt"`
	Port        int    `json:"port"`
	Rtspport    int    `json:"rtspport"`
	Type        int    `json:"type"`
	Username    string `json:"username"`
}

func (n *NVR) String() string {
	buf, _ := json.Marshal(n)

	return string(buf)
}


